model{
  for(i in 1:n.obs){ #ragged array/unbalanced panel
    d.save[i] ~ dnorm(mu[i], tau.y[i])
      mu[i] <- a.unit[country[i]] + 
        b.l.save[country[i]]*lag.save[i] +
        b.l.top1*lag.ineq[i] + b.d.top1*d.ineq[i]+   
        b.maj*maj[i] + b.l.top1maj*lag.ineq[i]*maj[i] + b.d.top1maj*d.ineq[i]*maj[i]+
        b.l.old*lag.old[i] + b.d.old*d.old[i] +
        b.l.gdppc*lag.gdppc[i] + b.d.gdppc*d.gdppc[i] +
        b.l.ltir*lag.ltir[i] + b.d.ltir*d.ltir[i]
    logsigma2.y[i] <- g.unit[country[i]] + g.year[years.d[i]] + g.euro*eurozone[i] #model for the variance
    tau.y[i] <- 1/exp(logsigma2.y[i])
    res[i]<-d.save[i] - mu[i] #residuals
    y.pred[i] ~ dnorm(mu[i], tau.y[i]) #repredicting y  
  }
  
  for(j in 1:(n.countries)){#country RE in model for variance  
    g.unit[j] ~ dnorm(0, tau.vc)
    a.unit[j] ~ dnorm(mu.c, tau.country) #country RE
    b.l.save[j] <- -1*temp[j]
    temp[j] ~ dbeta(1,1)
  }
  for(j in 1:n.years){#country RE in model for variance
    g.year[j] ~ dnorm(0, tau.vy) 
  }
  
# priors
  mu.c ~ dnorm(0,0.00001)
  sigma.country ~ dunif(0,50)
  tau.country <- pow(sigma.country,-2)
  sigma.vc ~ dunif(0,10) #variance for country RE in variance term
  sigma.vy ~ dunif(0,10) #variance for year RE in variance term
  tau.vc <- pow(sigma.vc, -2)
  tau.vy <- pow(sigma.vy, -2)
  b.l.top1~ dnorm(0,0.0001)
  b.d.top1~ dnorm(0,0.0001)
  b.maj ~ dnorm(0,0.0001)
  b.l.top1maj~ dnorm(0,0.0001)
  b.d.top1maj~ dnorm(0,0.0001)
  b.l.old ~ dnorm(0,0.0001)
  b.d.old ~ dnorm(0,0.0001)
  b.l.gdppc ~ dnorm(0,0.0001)
  b.d.gdppc ~ dnorm(0,0.0001)
  b.l.ltir ~ dnorm(0,0.0001)
  b.d.ltir ~ dnorm(0,0.0001)
  g.euro ~ dnorm (0,0.0001)
}

